<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    pieData: Object
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();

    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // console.log('el',this.$el);
      this.chart = echarts.init(this.$el, "macarons");
      const baseOption = {
        color: ["#37a2da", "#ffdb5c", "#67e0e3", "#ff9f7f", "#e062ae"],
        title: {
          backgroundColor: "#849fe2",
          textStyle: {
            color: "#fff",
            fontSize: 16
          }
        },
        tooltip: {
          // formatter: "{a} <br/>{b}: {c} ({d})"
          // formatter: function(value, index) {
          //   console.log("value", value, index);
          // }
        },
        grid: {},
        radar: {
          name: {
            textStyle: {
              color: "#ffffff",
              backgroundColor: "#37a2da"
            },
            padding: [5, 5, 5, 5],
            borderRadius: 3
          },
          radius: "50%",
          shape: "circle",
          splitLine: {
            lineStyle: {
              color: [
                "rgba(238, 197, 102, 0.1)",
                "rgba(238, 197, 102, 0.2)",
                "rgba(238, 197, 102, 0.4)",
                "rgba(238, 197, 102, 0.6)",
                "rgba(238, 197, 102, 0.8)",
                "rgba(238, 197, 102, 1)"
              ].reverse()
            }
          },
          splitArea: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: "rgba(238, 197, 102, 0.5)"
            }
          }
        },
        series: [
          {
            type: "radar",
            symbol: "none",
            name: "资产结构",
            itemStyle: {
              normal: {
                color: "#b1cfde"
              }
            },
            areaStyle: {
              normal: {
                opacity: 0.1
              }
            },
            lineStyle: {
              normal: {
                width: 1,
                opacity: 0.5
              }
            }
          }
        ]
      };

      this.chart.setOption(baseOption);
      this.chart.setOption(this.pieData);
    }
  }
};
</script>
